package com.kdtech.analyse.news;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class ZjstvNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {

		boolean bRet = false;

		String[] regex = {
				"http://www.zjstv.com/.*[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]+.html",
				"http://www.zjstv.com/[a-z]*/",
				"http://[a-z]*.zjstv.com/[a-z]*/[a-z]*",
				"http://www.zjstv.com/[a-z]*/[a-z]*/[0-9]*/[0-9]*.html",
				"http://[a-z]*.zjstv.com/c/[0-9]*/[0-9]*.html",
				"http://[a-z]*.zjstv.com/[a-z]*/[a-z]*/",
				"http://[a-z]*.zjstv.com/[a-z]*/[a-z]*/" };
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String html = urlMeta.getHtml();
		if (html == null) {
		}
		String title = "";
		String content = "";
		String author = "";
		Long date = 0l;
		int commentNum = 0;
		int clickNum = 0;
		NewsMeta meta = new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc = Jsoup.parse(html);
		urlMeta = null;
		/*
		 * 解析新闻标题
		 */
		title = doc.select("div.article_title").text();
		if (StringUtils.isBlank(title)) {
			title = doc.select("div.n_photo_tit").text();
			if (StringUtils.isBlank(title)) {
				title = doc.select("div#content h1").text();
			}
		}
		/*
		 * 解析时间
		 */
		date = DateUtils.matchDate(doc.select("div.article_time").text());
		if (date == null) {
			date = DateUtils.matchDate(doc.select("div.n_photo_ly2").text());
			if (date == null) {
				date = DateUtils.matchDate(url);
			}
		}
		/*
		 * 解析内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc
				.select("div.article_content"));
		if (StringUtils.isBlank(content)) {
			content = HtmlCleaner.getContentHtml(url, doc
					.select("div.ccontent1"));
			if (StringUtils.isBlank(content)) {
				content = HtmlCleaner.getContentHtml(url, doc
						.select("div#content p"));
				if (StringUtils.isBlank(content)) {
					content = HtmlCleaner.getContentHtml(url, doc
							.select("div#xx"));
				}
			}
		}

		/*
		 * 作者
		 */
		String authorStr = null;
		authorStr = doc.select("div.content_time span").text();
		if (StringUtils.isBlank(authorStr)) {
			authorStr = doc.select("div.article_time").text();
			if (StringUtils.isBlank(authorStr)) {
				authorStr = doc.select("div.xx1").text();
			}
		}
		author = StringUtils.substringBetween(authorStr, "来源：", " ");
		if (StringUtils.isBlank(author)) {
			author = StringUtils.substringAfter(authorStr, "来源：");
		}
		if (StringUtils.isBlank(author)) {
			author = StringUtils.substringAfter(authorStr, " ");
			author = StringUtils.substringAfter(author, " ");
		}

		if (StringUtils.isBlank(title)) {
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url = "http://www.zjstv.com/news/telenews/2014/01/2014-01-21308943.html";
		UrlMeta urlMeta = CrawlHTML.responseToURL(url);
		ZjstvNewsAnalyse a = new ZjstvNewsAnalyse();
		boolean detailPage = a.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml = a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}

	}

	
	public boolean isNeedUpdate() {
		return false;
	}
}
